﻿namespace ZhiAo.BaseFramework.Library.INIFile
{
    using System;
    using System.Collections.Specialized;
    using System.Runtime.InteropServices;
    using System.Text;

    public class INIWrapper
    {
        private INIWrapper()
        {
        }

        public static int GetINIInt(string filename, string section, string key)
        {
            int iDefault = -1;
            return GetPrivateProfileInt(section, key, iDefault, filename);
        }

        public static StringCollection GetINISection(string filename, string section)
        {
            StringCollection strings = new StringCollection();
            byte[] lpReturnedString = new byte[0x8000];
            int num = 0;
            num = GetPrivateProfileSection(section, lpReturnedString, lpReturnedString.GetUpperBound(0), filename);
            if (num > 0)
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < num; i++)
                {
                    if (lpReturnedString[i] != 0)
                    {
                        builder.Append((char) lpReturnedString[i]);
                    }
                    else if (builder.Length > 0)
                    {
                        strings.Add(builder.ToString());
                        builder = new StringBuilder();
                    }
                }
            }
            return strings;
        }

        public static StringCollection GetINISectionNames(string filename)
        {
            StringCollection strings = new StringCollection();
            byte[] lpReturnedString = new byte[0x8000];
            int num = 0;
            num = GetPrivateProfileSectionNames(lpReturnedString, lpReturnedString.GetUpperBound(0), filename);
            if (num > 0)
            {
                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < num; i++)
                {
                    if (lpReturnedString[i] != 0)
                    {
                        builder.Append((char) lpReturnedString[i]);
                    }
                    else if (builder.Length > 0)
                    {
                        strings.Add(builder.ToString());
                        builder = new StringBuilder();
                    }
                }
            }
            return strings;
        }

        public static string GetINIValue(string filename, string section, string key)
        {
            StringBuilder lpReturnedString = new StringBuilder(0x100);
            string lpDefault = "";
            if (GetPrivateProfileString(section, key, lpDefault, lpReturnedString, lpReturnedString.Capacity, filename) != 0)
            {
                return lpReturnedString.ToString();
            }
            return null;
        }

        [DllImport("KERNEL32.DLL")]
        protected internal static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int iDefault, string lpFileName);
        [DllImport("KERNEL32.DLL")]
        protected internal static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string lpFileName);
        [DllImport("KERNEL32.DLL")]
        protected internal static extern int GetPrivateProfileSectionNames(byte[] lpReturnedString, int nSize, string lpFileName);
        [DllImport("KERNEL32.DLL")]
        protected internal static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
        public static bool WriteINISection(string filename, string section, StringCollection items)
        {
            byte[] bytes = new byte[0x8000];
            int byteIndex = 0;
            foreach (string str in items)
            {
                Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, byteIndex);
                byteIndex += str.Length;
                bytes[byteIndex] = 0;
                byteIndex++;
            }
            bytes[byteIndex] = 0;
            return WritePrivateProfileSection(section, bytes, filename);
        }

        public static bool WriteINIValue(string filename, string section, string key, string sValue)
        {
            return WritePrivateProfileString(section, key, sValue, filename);
        }

        [DllImport("KERNEL32.DLL")]
        protected internal static extern bool WritePrivateProfileSection(string lpAppName, byte[] data, string lpFileName);
        [DllImport("KERNEL32.DLL")]
        protected internal static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
    }
}

